﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.dal;
namespace bzxxxt.admin.service
{
    public class LoginLogService : BaseService
    {
        
        public void add(string userName, string userIp, string city, bool success) {
            tbLoginLog tb = new tbLoginLog
            {
                userName = userName,
                userIp = userIp,
                city = city,
                success = success,
                loginDate = DateTime.Now,
            };
            DB.tbLoginLog.Add(tb);
            DB.SaveChanges();
        }

        public string getall()
        {
            var list = from b in DB.tbLoginLog                       
                       orderby b.loginDate descending
                       select new LoginLogEntity
                       {
                           id = b.id,
                           userName = b.userName,
                           userIp = b.userIp,
                           city = b.city,
                           success = b.success,
                           loginDate = b.loginDate
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where)
        {
            int totalCount = 0;
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.tbLoginLog.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("tbLoginLog", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var list = from b in pager
                       orderby b.loginDate descending
                       select new LoginLogEntity
                       {
                           id = b.id,
                           userName = b.userName,
                           userIp = b.userIp,
                           city = b.city,
                           success = b.success,
                           loginDate = b.loginDate
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string delete(string strIds)
        {
            List<int> ids = new List<int>();
            if (!string.IsNullOrEmpty(strIds))
            {
                string[] arr = strIds.Split(',');
                foreach (var id in arr)
                {
                    ids.Add(int.Parse(id));
                }
            }
            var logs = DB.tbLoginLog.Where(m => ids.Contains(m.id)).ToList();
            if (logs != null)
            {
                logs.ForEach(m => DB.tbLoginLog.Remove(m));
                DB.SaveChanges();
            }
            return JsonUtil.ToJson(new { success = true });
        }
    }
}